package com.qf.smartdevice.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.github.pagehelper.PageInfo;
import com.qf.smartdevice.constans.ResultCode;
import com.qf.smartdevice.dto.R;
import com.qf.smartdevice.exceptions.AddDataException;
import com.qf.smartdevice.exceptions.UpdateException;
import com.qf.smartdevice.pojo.Devices;
import com.qf.smartdevice.service.ICategoriesService;
import com.qf.smartdevice.service.IDevicesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/devices")
public class DeviceController {

    @Autowired
    private IDevicesService service;

    @GetMapping("/devices")
    public R findAllDevices(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "100") Long areasearch, @RequestParam(defaultValue = "10") int limit, String address, @RequestParam(defaultValue = "-100") Long areaid, @RequestParam(defaultValue = "-100") Long categoryid, @RequestParam(defaultValue = "-100") Long status) {
        System.out.println("哈哈哈哈哈哈");

        PageInfo<Devices> allDevices = service.findAllDevices(page, limit, address, areaid, categoryid, status, areasearch);


        return R.setOk(allDevices);

    }

    @DeleteMapping("/devices")
    public R deleteDevicesByIds(int[] ids) {
        int i = service.devicesById(ids);
        return R.setOk(null);
    }

    @PostMapping("/device")
    public R insertDevice(@RequestBody Devices devices) {
//        String status = devices.getStatus();


        if (devices == null || devices.isNull()) {
            throw new AddDataException("数据不完整", ResultCode.NULL_DATA);
        }

        int i = service.insertDevices(devices);
        return R.setOk(devices);
    }

    @PutMapping("/device")
    public R updatetDevice(@RequestBody Devices devices) {
        if (devices == null || devices.isNull()) {
            throw new UpdateException("数据不完整", ResultCode.NULL_DATA);
        }
        int i = service.updateByIdDevice(devices);
        return R.setOk(null);
    }

    @PostMapping("/command/{uuid}/{command}")
    public R sendControl(@PathVariable String uuid, @PathVariable String command) throws JsonProcessingException {
        service.sendControl(uuid, command);
        return R.setOk(null);
    }
}